//
//  String+Regex.swift
//  0225-Rwgex
//
//  Created by 孙明海 on 16/2/25.
//  Copyright © 2016年 孙明海. All rights reserved.
//

import Foundation

extension String {
    func href() -> (link: String, text: String)? {
        let pattern = "<a href=\"(.*?)\" .*?>(.*?)</a>"
        let regex = try! NSRegularExpression(pattern: pattern, options: [])
        
        guard let result = regex.firstMatchInString(self, options: [], range: NSRange(location: 0, length: self.characters.count)) else {
            print("没有找到匹配项")
            return nil
        }

        let str = self as NSString
        
        let r1 = result.rangeAtIndex(1)
        let link = str.substringWithRange(r1)
        
        let r2 = result.rangeAtIndex(2)
        let text = str.substringWithRange(r2)
        return (link, text)
    }
}